package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.domain.WmsStockOutDetails;
import com.ruoyi.wms.mapper.WmsStockOutMapper;
import com.ruoyi.wms.domain.WmsStockOut;
import com.ruoyi.wms.service.IWmsStockOutService;

/**
 * 出库单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-17
 */
@Service
public class WmsStockOutServiceImpl implements IWmsStockOutService 
{
    @Autowired
    private WmsStockOutMapper wmsStockOutMapper;

    /**
     * 查询出库单管理
     * 
     * @param stockOutUuid 出库单管理主键
     * @return 出库单管理
     */
    @Override
    public WmsStockOut selectWmsStockOutByStockOutUuid(String stockOutUuid)
    {
        return wmsStockOutMapper.selectWmsStockOutByStockOutUuid(stockOutUuid);
    }

    /**
     * 查询出库单管理列表
     * 
     * @param wmsStockOut 出库单管理
     * @return 出库单管理
     */
    @Override
    public List<WmsStockOut> selectWmsStockOutList(WmsStockOut wmsStockOut)
    {
        return wmsStockOutMapper.selectWmsStockOutList(wmsStockOut);
    }

    /**
     * 新增出库单管理
     * 
     * @param wmsStockOut 出库单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsStockOut(WmsStockOut wmsStockOut)
    {
        wmsStockOut.setCreateTime(DateUtils.getNowDate());
        int rows = wmsStockOutMapper.insertWmsStockOut(wmsStockOut);
        insertWmsStockOutDetails(wmsStockOut);
        return rows;
    }

    /**
     * 修改出库单管理
     * 
     * @param wmsStockOut 出库单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsStockOut(WmsStockOut wmsStockOut)
    {
        wmsStockOut.setUpdateTime(DateUtils.getNowDate());
        wmsStockOutMapper.deleteWmsStockOutDetailsByFkStockOutUuid(wmsStockOut.getStockOutUuid());
        insertWmsStockOutDetails(wmsStockOut);
        return wmsStockOutMapper.updateWmsStockOut(wmsStockOut);
    }

    /**
     * 批量删除出库单管理
     * 
     * @param stockOutUuids 需要删除的出库单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsStockOutByStockOutUuids(String[] stockOutUuids)
    {
        wmsStockOutMapper.deleteWmsStockOutDetailsByFkStockOutUuids(stockOutUuids);
        return wmsStockOutMapper.deleteWmsStockOutByStockOutUuids(stockOutUuids);
    }

    /**
     * 删除出库单管理信息
     * 
     * @param stockOutUuid 出库单管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsStockOutByStockOutUuid(String stockOutUuid)
    {
        wmsStockOutMapper.deleteWmsStockOutDetailsByFkStockOutUuid(stockOutUuid);
        return wmsStockOutMapper.deleteWmsStockOutByStockOutUuid(stockOutUuid);
    }

    /**
     * 新增出库单明细信息
     * 
     * @param wmsStockOut 出库单管理对象
     */
    public void insertWmsStockOutDetails(WmsStockOut wmsStockOut)
    {
        List<WmsStockOutDetails> wmsStockOutDetailsList = wmsStockOut.getWmsStockOutDetailsList();
        String stockOutUuid = wmsStockOut.getStockOutUuid();
        if (StringUtils.isNotNull(wmsStockOutDetailsList))
        {
            List<WmsStockOutDetails> list = new ArrayList<WmsStockOutDetails>();
            for (WmsStockOutDetails wmsStockOutDetails : wmsStockOutDetailsList)
            {
                wmsStockOutDetails.setFkStockOutUuid(stockOutUuid);
                list.add(wmsStockOutDetails);
            }
            if (list.size() > 0)
            {
                wmsStockOutMapper.batchWmsStockOutDetails(list);
            }
        }
    }
}
